<template>
  <div
    :class="className"
    :style="{ height: height, width: width }"
    id="eChart"
  />
</template>

<script>
import echarts from 'echarts'
import elementResizeDetectorMaker from 'element-resize-detector'
require('echarts/theme/macarons') // echarts theme
import resize from '@/components/mixins/resize'

const animationDuration = 2800

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        console.log(val)
        this.setOptions(val)
      }
    }
  },
  mounted() {
    const that = this
    var echart = document.getElementById('eChart')
    this.$nextTick(() => {
      this.initChart()
    })
    var erd = elementResizeDetectorMaker()
    erd.listenTo(echart, function() {
      that.$nextTick(function() {
        echarts.init(echart).resize()
      })
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions(chartData) {
      //   console.log(chartData)
      const data_times = chartData.dates
      const data_valus = chartData.values
      const data_name = []
      for (const i in data_valus) {
        data_name.push(data_valus[i].name)
      }
      this.chart.setOption({
        xAxis: {
          data: data_times,
          boundaryGap: false,
          axisLabel: {
            interval: 0,
            textStyle: {
              fontSize: 10
            }
          },
          axisTick: {
            show: false
          }
        },
        grid: {
          left: 10,
          right: 30,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: {
          axisTick: {
            show: false
          }
        },
        legend: {
          data: data_name
        },
        series: [
          {
            name: data_valus[0].name,
            itemStyle: {
              normal: {
                color: '#40c9c6',
                lineStyle: {
                  color: '#40c9c6',
                  width: 2
                }
              }
            },
            smooth: true,
            type: 'line',
            data: data_valus[0].datas,
            animationDuration,
            animationEasing: 'cubicInOut'
          },
          {
            name: data_valus[1].name,
            smooth: true,
            type: 'line',
            itemStyle: {
              normal: {
                color: '#36a3f7',
                lineStyle: {
                  color: '#36a3f7',
                  width: 2
                }
              }
            },
            data: data_valus[1].datas,
            animationDuration,
            animationEasing: 'quadraticOut'
          },
          {
            name: data_valus[2].name,
            smooth: true,
            type: 'line',
            itemStyle: {
              normal: {
                color: '#b6a2de',
                lineStyle: {
                  color: '#b6a2de',
                  width: 2
                }
                // areaStyle: {
                //   color: '#f3f8ff'
                // }
              }
            },
            data: data_valus[2].datas,
            animationDuration,
            animationEasing: 'quadraticOut'
          }
        ]
      })
    }
  }
}
</script>
